import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';
import { Storage } from '@ionic/storage';

import { EmailIndexPage } from '../../email/index/index.component';
import { InfoListPage } from '../../info/list/list.component';

import { AuthorizationListPage } from '../../authorization/list/list.component';
import { TaskListPage } from '../../task/list/list.component';
import { FlowListPage } from '../../flow/list/list.component';
import { DraftListPage } from '../../draft/list/list.component';
import { ArchivePage } from '../../archive/archive.component';
import { MarketingIndexPage } from '../../marketing/index/index.component';
import { WorkIndexService } from "./index.service";
import { IpmIndexPage } from "../../ipm/index/index.component";
import { GfrNetValuePage } from "../../gfr/netvalue/netvalue.component";
import _ from 'lodash';
import { MarketingHomePage } from "../../marketing/home/home.component";
import { MarketingMobilePage } from "../../marketing/mobile/mobile.component";
import { MarketingDailyListPage } from "../../marketing/daily/list/list.component";
import { MarketingWeeklyListPage } from "../../marketing/weekly/list/list.component";
import { MarketingLineupListPage } from "../../marketing/lineup/list/list.component";
import { IpmCirculatePage } from "../../ipm/circulate/circulate.component";
import { IpmCashPage } from "../../ipm/cash/cash.component";
import { IpmCoinPage } from "../../ipm/coin/coin.component";
import { BcwHomePage } from "../../bcw/home/home.component";
import { MarketingCreditListPage } from "../../marketing/credit/list/list.component"
import { BcwRulePage } from "../../bcw/rule/rule.component";
import { IrpQjtHomePage } from "../../irp/panoramagram/home/home.component";
import { BcwWarnPage } from "../../bcw/warn/warn.component";
import { IrpPanoramagramMainPage } from "../../irp/panoramagram/main/main.component";
import { IrpQjtpersonDetailPage } from "../../irp/panoramagram/personDetail/personDetail.component";
import { StockRecommendPage } from "../../irp/stock-recommend/stock-recommend.component";
import { MorningStockRecommendPage } from "../../irp/morningStock-recommend/morningStock-recommend.component";
import { StrategyRecommendPage } from "../../irp/strategy-recommend/strategy-recommend";
import { MarketingMobileAnalyListPage } from "../../marketing/mobileanaly/list/list.component";
import { MarketingInstitutionPage } from "../../marketing/institution/institution.component";
import { MarketingMutualFundWeeklyPage } from "../../marketing/mfweekly/mfweekly.component";
import { MarketingMutualFundMonthlyPage } from "../../marketing/mfmonthly/mfmonthly.component";
import { MarketingProductionMangePage } from "../../marketing/manage/mange.component";
import { IrpSchedulerListPage } from "../../irp/scheduler/list/list.component";
import { IrpHotStockHomePage } from "../../irp/hotStock/home/home.component";
import { IrpPoolListPage } from "../../irp/pool/list/list.component";
import { IrpPositionListPage } from "../../irp/position/list/list.component";
import { Irpreportmainpage } from "../../irp/report/main/main.component";

@Component({
  selector: 'page-work-index',
  templateUrl: 'index.html',
  providers: [WorkIndexService]
})
export class WorkIndexPage {
  moa_page_name: any = "page-work-index";
  moa_page_explain: any = "工作台";

  isEdit = false;
  pages: any = [{
    name: "EmailIndexPage",
    page: EmailIndexPage
  }, {
    name: "InfoListPage",
    page: InfoListPage
  }, {
    name: "AuthorizationListPage",
    page: AuthorizationListPage
  }, {
    name: "TaskListPage",
    page: TaskListPage
  }, {
    name: "FlowListPage",
    page: FlowListPage
  }, {
    name: "DraftListPage",
    page: DraftListPage
  }, {
    name: "ArchivePage",
    page: ArchivePage
  }, {
    name: "MarketingIndexPage",
    page: MarketingIndexPage
  }, {
    name: "IpmIndexPage",
    page: IpmIndexPage
  }, {
    name: "GfrNetValuePage",
    page: GfrNetValuePage
  }, {
    name: "MarketingHomePage",
    page: MarketingHomePage
  }, {
    name: "MarketingMobilePage",
    page: MarketingMobilePage
  }, {
    name: "MarketingDailyListPage",
    page: MarketingDailyListPage
  }, {
    name: "MarketingWeeklyListPage",
    page: MarketingWeeklyListPage
  }, {
    name: "MarketingLineupListPage",
    page: MarketingLineupListPage
  }, {
    name: "MarketingCreditListPage",
    page: MarketingCreditListPage
  }, {
    name: "IpmCirculatePage",
    page: IpmCirculatePage
  }, {
    name: "IpmCashPage",
    page: IpmCashPage
  }, {
    name: "IpmCoinPage",
    page: IpmCoinPage
  }, {
    name: "BcwHomePage",
    page: BcwHomePage
  }, {
    name: "BcwRulePage",
    page: BcwRulePage
  }, {
    name: "IrpPanoramagramMainPage",
    page: IrpPanoramagramMainPage
  }, {
    name: "IrpQjtHomePage",
    page: IrpQjtHomePage
  }, {
    name: "BcwWarnPage",
    page: BcwWarnPage
  }, {
    name: "IrpHotStockHomePage",
    page: IrpHotStockHomePage
  }, {
    name: "IrpQjtpersonDetailPage",
    page: IrpQjtpersonDetailPage
  }, {
    name: "MarketingMobileAnalyListPage",
    page: MarketingMobileAnalyListPage
  }, {
    name: "MarketingInstitutionPage",
    page: MarketingInstitutionPage
  }, {
    name: "MarketingMutualFundWeeklyPage",
    page: MarketingMutualFundWeeklyPage
  }, {
    name: "MarketingMutualFundMonthlyPage",
    page: MarketingMutualFundMonthlyPage
  }, {
    name: "MarketingProductionMangePage",
    page: MarketingProductionMangePage
  }, {
    name: "StockRecommendPage",
    page: StockRecommendPage
  }, {
    name: "MorningStockRecommendPage",
    page: MorningStockRecommendPage
  }, {
    name: "StrategyRecommendPage",
    page: StrategyRecommendPage
  }, {
    name: "MarketingMobileAnalyListPage",
    page: MarketingMobileAnalyListPage
  }, {
    name: "MarketingInstitutionPage",
    page: MarketingInstitutionPage
  }, {
    name: "MarketingMutualFundWeeklyPage",
    page: MarketingMutualFundWeeklyPage
  }, {
    name: "MarketingMutualFundMonthlyPage",
    page: MarketingMutualFundMonthlyPage
  }, {
    name: "MarketingProductionMangePage",
    page: MarketingProductionMangePage
  }, {
    name: "IrpSchedulerListPage",
    page: IrpSchedulerListPage
  }, {
    name: "IrpPositionListPage",
    page: IrpPositionListPage
  },
  {
    name: "BcwWarnPage",
    page: BcwWarnPage
  }, {
    name: "MarketingMobileAnalyListPage",
    page: MarketingMobileAnalyListPage
  }, {
    name: "MarketingMobileAnalyListPage",
    page: MarketingMobileAnalyListPage
  }, {
    name: "MarketingInstitutionPage",
    page: MarketingInstitutionPage
  }, {
    name: "MarketingMutualFundWeeklyPage",
    page: MarketingMutualFundWeeklyPage
  }, {
    name: "MarketingMutualFundMonthlyPage",
    page: MarketingMutualFundMonthlyPage
  }, {
    name: "MarketingProductionMangePage",
    page: MarketingProductionMangePage
  }, {
    name: "IrpSchedulerListPage",
    page: IrpSchedulerListPage
  }, {
    name: "IrpPositionListPage",
    page: IrpPositionListPage
  }, {
    name: "IrpPoolListPage",
    page: IrpPoolListPage
  },
  {
    name: "Irpreportmainpage",
    page: Irpreportmainpage
  }];

  _ = _;
  menu: any = [];
  sortable: any = null;
  regularMenu: any = [];

  constructor(public navCtrl: NavController,
    public indexService: WorkIndexService,
    public storage: Storage) {

    this.storage.get("configs").then(configs => {
      if (configs && configs.length > 0) {
        this.regularMenu = _.find(configs, {
          name: "regularMenu"
        })['json'];
        this.initMenu();
      }
    });

  }

  /**
   * 初始化模块
   */
  initMenu() {
    let that = this;
    this.storage.get("myMenu").then(storeMyMenu => {
      if (storeMyMenu) {
        var tempMenu = [];
        _.each(storeMyMenu, function (item, index) {
          var module: any = (_.find(that.regularMenu, {
            id: item.split('-')[0]
          }));
          if (!module.isHidden) {
            var temp: any = _.find(module.list, {
              id: item
            });
            if (temp && !temp.isHidden) {
              tempMenu.push(temp);
            }
          }
        });
        var module: any = (_.find(that.regularMenu, {
          id: 'my'
        }));
        module.list = tempMenu;
      }
      this.menu = that.regularMenu;
    });
  }

  /**
   * 开始编辑图标
   */
  beginEdit() {
    if (!this.isEdit) {
      console.log('---beginEdit---');

      this.isEdit = true;
      this.initSortable();
    }
  }

  initSortable() {
    if (this.sortable) {
      this.sortable.option("disabled", false);
    } else {
      let that = this;
      let workModuleContent = document.getElementsByClassName('work-module-content');
      this.sortable = new window['Sortable'](workModuleContent[0], {
        animation: 300,
        scroll: false,
        group: "work-group",
        dataIdAttr: 'data-id',
        draggable: ".work-item",
        // Element dragging ended
        onEnd: function (/**Event*/evt) {
          // that.sortable.destroy();
          // that.initSortable();
        },
      });
    }
  };

  /**
   * 完成编辑图标
   */
  completeEdit() {
    let newMenu = this.sortable.toArray();
    this.indexService.editMenu(newMenu).subscribe(data => {
      if (data) {
        this.isEdit = false;
        this.storage.set("myMenu", newMenu);
        this.sortable.option("disabled", true);
        // this.sortable.destroy();
      }
    });
  }

  addToMy(item) {
    this.menu[0].list.push(item);
  }

  removeFromMy(item) {
    _.remove(this.menu[0].list, { id: item.id });
  }

  openPage(item) {
    if (!this.isEdit) {
      let parmas = {};
      if (item.params) {
        parmas = item.params;
      }
      let page: any = _.find(this.pages, {
        name: item.url
      });
      console.log(page);
      if (page) {
        this.navCtrl.push(page.page, parmas);
      }
    }
  }
}

